package com.dempsey.voronoiweb.project.controller;

import com.dempsey.voronoiweb.project.entity.Cover;
import com.dempsey.voronoiweb.project.entity.DelaunayTriangle;
import com.dempsey.voronoiweb.project.entity.Edge;
import com.dempsey.voronoiweb.project.entity.Point;
import com.dempsey.voronoiweb.utils.CreateDelaunayTriangleMap;
import com.dempsey.voronoiweb.utils.CreateVoronoiMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Dempsey
 * @Date: 2021/2/25 12:49
 * @Description:
 */
@RestController
@RequestMapping("/algorithm")
public class DistributedDomainSolver {
    /**
     * 区域分割算法
     *
     * @param centerPoint 区域的中心点，本项目中代表失踪地
     * @param radius      区域的大小，以半径表示
     * @param mainPoints  区域划分的基点，本项目中代表失踪老人常去的地方
     * @return the map
     */
    @RequestMapping("/distributedDomainSolver")
    public static List<Cover> distributedDomainSolver(Point centerPoint, Double radius, List<Point> mainPoints) {

        // 给基点排序，以经度为主序，纬度为辅序
        List<Point> mps = mainPoints.stream()
                .sorted(Comparator.comparing(Point::getLongitude).thenComparing(Point::getLatitude))
                .collect(Collectors.toList());

        //构建 Delaunay 三角网
        //确定的三角形列表
        List<DelaunayTriangle> triangles = new ArrayList<>();
        //边缘边集
        List<Edge> broderEdges = new ArrayList<>();
        //边缘点集
        List<Point> broderPoints = new ArrayList<>();

        CreateDelaunayTriangleMap.createDelanuaryTriangleMap(triangles, broderEdges, broderPoints, mps);

        //绘制维诺图
        List<Cover> covers = CreateVoronoiMap.createVoronoiMap(centerPoint, radius, triangles, broderEdges, broderPoints, mps);

        return covers;
    }

}
